package com.pluto.securitydemo.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pluto.securitydemo.entity.User;
import com.pluto.securitydemo.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;

@Component
public class DBUserDetailsManager implements UserDetailsManager, UserDetailsPasswordService {

    @Resource
    private UserMapper userMapper;


    @Override
    public UserDetails updatePassword(UserDetails user, String newPassword) {
        return null;
    }

    /**
     *
     * @param userDetails
     */
    @Override
    public void createUser(UserDetails userDetails) {
        User user = new User();
        user.setUsername(userDetails.getUsername());
        user.setPassword(userDetails.getPassword());
        user.setEnabled(true);
        userMapper.insert(user);
    }

    @Override
    public void updateUser(UserDetails user) {

    }

    @Override
    public void deleteUser(String username) {

    }

    @Override
    public void changePassword(String oldPassword, String newPassword) {

    }

    @Override
    public boolean userExists(String username) {
        return false;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        User user = userMapper.selectOne(wrapper);
        if (user == null){
            throw new UsernameNotFoundException(username);
        }else {
            // Collection<GrantedAuthority> authorities = new ArrayList<>();
            // // 正常来说，user 和 role 应该在数据库存在两张表
            // // 通过维护 role 表，获取对应的 role 能力。
            // // 这里是通过硬编码的方式，指定了 user 具有  USER_LIST,USER_ADD 的权限
            // authorities.add(new GrantedAuthority() {
            //     @Override
            //     public String getAuthority() {
            //         return "USER_LIST";
            //     }
            // });
            // authorities.add(new GrantedAuthority() {
            //     @Override
            //     public String getAuthority() {
            //         return "USER_ADD";
            //     }
            // });

            // return new org.springframework.security.core.userdetails.User(
            //         user.getUsername(),
            //         user.getPassword(),
            //         user.getEnabled(),
            //         true, // 用户账号是否过期
            //         true, // 用户凭证是否过期
            //         true, // 用户是否未被锁定
            //         authorities);
            return org.springframework.security.core.userdetails.User
                    .withUsername(user.getUsername())
                    .password(user.getPassword())
                    .disabled(!user.getEnabled())
                    .credentialsExpired(false)
                    .accountLocked(false)
                    .roles("ADMIN")
                    .build();
        }
    }
}
